package com.szw.dlyk.web;
import com.github.pagehelper.PageInfo;
import com.szw.dlyk.domain.User;
import com.szw.dlyk.dto.UserDtoLogin;
import com.szw.dlyk.query.UserQuery;
import com.szw.dlyk.result.Result;
import com.szw.dlyk.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @program: dlyk
 * @description: 用户模块
 * @author: Songzw
 * @create: 2024-01-26 12:55
 **/
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取登录用户信息
     * @param authentication
     * @return
     */
    @GetMapping("/api/login/info")
    public Result loginInfo(Authentication authentication){
        UserDtoLogin user = (UserDtoLogin) authentication.getPrincipal();
        return Result.ok(user);
    }

    /**
     * 免登录
     * @return
     */
    @GetMapping("/api/login/free")
    public Result loginFree(){
        return Result.ok();
    }

    /**
     * 用户分页查询
     * @param current
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:list')")
    @GetMapping("/api/users")
    public Result getUsersByPage(@RequestParam(value = "current",required = false)Integer current){
        if (current == null){
            current = 1;
        }
        PageInfo<User> usersByPage = userService.getUsersByPage(current);
        return Result.ok(usersByPage);
    }

    @PreAuthorize(value = "hasAuthority('user:view')")
    @GetMapping("/api/user/{id}")
    public Result getUserDetail(@PathVariable(value = "id")Integer id){
        User user = userService.getUserDetailById(id);
        return Result.ok(user);
    }

    /**
     * 新增用户
     * @param userQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:add')")
    @PostMapping("/api/user")
    public Result addUser(UserQuery userQuery){
        int i = userService.saveUser(userQuery);
        System.out.println("i-------------------------------------"+i);
        return i >= 1? Result.ok():Result.fail();
    }

    /**
     * 编辑用户
     * @param userQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('user:edit')")
    @PutMapping("/api/user")
    public Result editUser(UserQuery userQuery){
        int i = userService.updateUser(userQuery);
        System.out.println("i-------------------------------------"+i);
        return i >= 1? Result.ok():Result.fail();
    }


    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping("/api/user/{id}")
    public Result deleteUser(@PathVariable(value = "id")Integer id){
         int i = userService.deleteUserById(id);
        return i >= 1? Result.ok():Result.fail();
    }

    @PreAuthorize(value = "hasAuthority('user:delete')")
    @DeleteMapping("/api/user/")
    public Result batchDeleteUser(@RequestParam(value = "ids")String ids){
        // ids:"1,3,5,7"
        List<String> idList = Arrays.asList(ids.split(","));
        int i = userService.batchDeleteUser(idList);
        return i >= idList.size()? Result.ok():Result.fail();
    }

    @GetMapping(value = "/api/owner/")
    public Result getOwner(){
        List<User> ownerList = userService.getOwnerList();
        return Result.ok(ownerList);
    }

}
